package com.mytest.demo.mongodb;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mytest.demo.entity.User;

@RestController
@RequestMapping("/mg/user")
public class MongoController {
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@GetMapping
	public String findAll() {
		
		List<User>ls=mongoTemplate.findAll(User.class);
		return JSON.toJSONString(ls);
	}
	
	@RequestMapping("/add")
	public String addUser(User user) {
		mongoTemplate.save(user);
		
		return "added";
	}
}
